{extend name="base/form" /}
{block name="title"} 首页 {/block}
<!-- Page Content -->
{block name="content"}
<form class="layui-form" action="{:url('addPost')}" method="post">
    <input type="hidden" name="id" value="{$id}">
<div id="test12" class="demo-tree-more"></div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button style="margin-left: 20%" class="layui-btn" lay-submit="" lay-filter="addSubmit">提交</button>
            <button id="reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!-- /Page Content -->
{/block}
{block name="js"}
<script>
    layui.use(['tree', 'util','form'], function(){
        var form = layui.form;
        var $ = layui.jquery
        $.ajax({
            type: "POST",
            async:false,
            url: "{:url('authData')}?id="+"{$id}",
            success: function (e) {
                 data = e.data;
            }
        })
        var tree = layui.tree
            ,layer = layui.layer
            //模拟数据
            ,data = data

        //实例化tree
        tree.render({
            elem: '#test12'
            ,data: data
            ,showCheckbox: true  //是否显示复选框
            ,id: 'id'
            ,field:'title'
            ,isJump: true //是否允许点击节点时弹出新窗口跳转
        });

        //监听提交
        form.on('submit(addSubmit)', function(data){
            var checkData = tree.getChecked('id');
            rules = getChecked_list(checkData);
            console.log(rules);
            console.log(data.field.id);
            $.ajax({
                url:"{:url('addAuthPost')}",
                type:'post',
                data:{'id':data.field.id,'rules':rules},
                success:function(e){
                    if(e.code == 1){
                        layer.msg(e.msg,{icon:e.icon,time:2000},function(){
                            //可以重载所有基础参数
                            tree.reload('id', {
                                //新的参数
                            });
                            window.location.reload()
                        })
                    }else{
                        layer.msg(e.msg,{icon:e.icon,time:2000},function (){
                            //可以重载所有基础参数
                            tree.reload('id', {
                                //新的参数
                            });
                            window.location.reload()
                        })
                    }
                }
            });
            return false;
        });

        // 处理获取选中ID
        function getChecked_list(data) {
            var id = "";
            $.each(data, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                } else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            });
            return id;
        }

    });
</script>

{/block}